package a4;

import com.common.ServerBootstrap;
import com.common.ChannelFuture;
import com.common.ChannelInitializer;
import com.common.ChannelOption;
import com.common.EventLoopGroup;
import com.common.NioEventLoopGroup;
import com.common.SocketChannel;
import com.common.NioServerSocketChannel;
import com.common.http.DelimiterBasedFrameDecoder;
import com.common.http.Delimiters;
import com.common.StringEncoder;

public class NettyServer {
	
	public static void main(String[] args) {
		EventLoopGroup parentGroup = new NioEventLoopGroup(1);
		EventLoopGroup childGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(parentGroup, childGroup);
			serverBootstrap.channel(NioServerSocketChannel.class);
			serverBootstrap.option(ChannelOption.SO_BACKLOG, 128)
			                         .option(ChannelOption.SO_KEEPALIVE, true) 
			                         .childHandler(new ChannelInitializer<SocketChannel>() {
										@Override
										protected void initChannel(SocketChannel ch)
												throws Exception {
											ch.pipeline().addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE,Delimiters.lineDelimiter()[0]));
											ch.pipeline().addLast(new SimpleHandler());
											ch.pipeline().addLast(new StringEncoder());
										}
									});
			
			ChannelFuture future = serverBootstrap.bind(8080).sync();
			future.channel().closeFuture().sync();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			parentGroup.shutdownGracefully();
			childGroup.shutdownGracefully();
		}
		
		
		
		
	}

}
